/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminGUI.java
 *
 * Created on Oct 17, 2011, 5:47:01 PM
 */
package admingui;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author galen
 */
public class AdminGUI extends javax.swing.JFrame {

    private TableModel myTableModel;
    private ArrayList<Item> items;
    private ArrayList<Item> itemsCopy;
    private AdminCommands admCommands;
    
    /** Creates new form AdminGUI */
    public AdminGUI() {
        items = new ArrayList<Item>();
        itemsCopy = new ArrayList<Item>();
        admCommands = new AdminCommands();
        myTableModel = new TableModel();
        updateList();
        initComponents();
        getMonth();
    }
    
    public final void updateList(){
        items = admCommands.getItems();
        itemsCopy.clear();
        for(int i = 0; i < items.size(); i++){
           itemsCopy.add(new Item(items.get(i)));
        }
        
        myTableModel.fireTableStructureChanged();
    }

    public final void getMonth(){
        String month = admCommands.adminGetMonth();
        monthLabel.setText(month);
    }

    public final void updateQuantity(){
        admCommands.updateQuantity(items, itemsCopy);
        this.updateList();
        myTableModel.fireTableStructureChanged();
    }


    private class TableModel extends AbstractTableModel{
        
        
        
        private String [] columnNames = {"ID", "Name", "Price", "Quantity", "Delete"};
        
        public int getRowCount() {
            return items.size();
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }
   

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            if(columnIndex == 3){
                return true;
            }
            
            else
                return false;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            
            switch (columnIndex){
                case 0:
                    return items.get(rowIndex).getID();
                case 1:
                    return items.get(rowIndex).getName();
                case 2:
                    return items.get(rowIndex).getPrice();
                case 3:
                    return items.get(rowIndex).getQuantity();
                case 4:
                    return false;
            }
            return null;
        }

        @Override
        public void setValueAt(Object value, int rowIndex, int columnIndex) {
            
            items.get(rowIndex).setQuantity(String.valueOf(value));
            fireTableCellUpdated(rowIndex, columnIndex);

        }
        
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addItemFrame = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        idTextField = new javax.swing.JTextField();
        quantityTextField = new javax.swing.JTextField();
        nameTextField = new javax.swing.JTextField();
        priceTextField = new javax.swing.JTextField();
        idLabe = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        quantityLabel = new javax.swing.JLabel();
        addItemFrameButton = new javax.swing.JButton();
        itemTablePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        itemTable = new javax.swing.JTable();
        addItemButton = new javax.swing.JButton();
        updateQuantityButton = new javax.swing.JButton();
        monthLabel = new javax.swing.JLabel();
        newMonthButton = new javax.swing.JButton();

        addItemFrame.setResizable(false);

        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });

        idLabe.setText("ID");

        nameLabel.setText("Name");

        priceLabel.setText("Price");

        quantityLabel.setText("Quantity");

        addItemFrameButton.setText("Add");
        addItemFrameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemFrameButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(idTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                    .addComponent(idLabe))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(priceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(priceLabel)
                        .addGap(40, 40, 40)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(quantityLabel)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(quantityTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                        .addGap(37, 37, 37))))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(111, 111, 111)
                .addComponent(addItemFrameButton)
                .addContainerGap(139, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(idLabe)
                    .addComponent(priceLabel)
                    .addComponent(quantityLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(priceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(idTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(quantityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addComponent(addItemFrameButton)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout addItemFrameLayout = new javax.swing.GroupLayout(addItemFrame.getContentPane());
        addItemFrame.getContentPane().setLayout(addItemFrameLayout);
        addItemFrameLayout.setHorizontalGroup(
            addItemFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addItemFrameLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        addItemFrameLayout.setVerticalGroup(
            addItemFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addItemFrameLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        itemTable.setModel(myTableModel);
        jScrollPane1.setViewportView(itemTable);

        addItemButton.setText("Add Item");
        addItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemButtonActionPerformed(evt);
            }
        });

        updateQuantityButton.setText("Update Quantity");
        updateQuantityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateQuantityButtonActionPerformed(evt);
            }
        });

        monthLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        newMonthButton.setText("New Month");
        newMonthButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMonthButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout itemTablePanelLayout = new javax.swing.GroupLayout(itemTablePanel);
        itemTablePanel.setLayout(itemTablePanelLayout);
        itemTablePanelLayout.setHorizontalGroup(
            itemTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemTablePanelLayout.createSequentialGroup()
                .addGroup(itemTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(itemTablePanelLayout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(monthLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(newMonthButton)
                        .addGap(37, 37, 37)
                        .addComponent(addItemButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(updateQuantityButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, itemTablePanelLayout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        itemTablePanelLayout.setVerticalGroup(
            itemTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(itemTablePanelLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(itemTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(itemTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(newMonthButton)
                        .addComponent(updateQuantityButton)
                        .addComponent(addItemButton))
                    .addComponent(monthLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(itemTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(445, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(itemTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_nameTextFieldActionPerformed

private void addItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemButtonActionPerformed

    addItemFrame.setLocationRelativeTo(this);
    addItemFrame.setSize(300, 200);
    addItemFrame.setVisible(true);
    
}//GEN-LAST:event_addItemButtonActionPerformed

private void addItemFrameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemFrameButtonActionPerformed

    String id, name, price, quantity;
    id = idTextField.getText();
    name = nameTextField.getText();
    price = priceTextField.getText();
    quantity = quantityTextField.getText();
    admCommands.addItem(id, name, price, quantity);
    System.out.println(id + " " + name + " " + price + " " + quantity);
    addItemFrame.setVisible(false);
    idTextField.setText("");
    quantityTextField.setText("");
    nameTextField.setText("");
    priceTextField.setText("");
    this.updateList();
}//GEN-LAST:event_addItemFrameButtonActionPerformed

private void updateQuantityButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateQuantityButtonActionPerformed
    this.updateQuantity();
}//GEN-LAST:event_updateQuantityButtonActionPerformed

private void newMonthButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMonthButtonActionPerformed
    admCommands.updateMonthAdmin();
    this.getMonth();
}//GEN-LAST:event_newMonthButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdminGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AdminGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addItemButton;
    private javax.swing.JFrame addItemFrame;
    private javax.swing.JButton addItemFrameButton;
    private javax.swing.JLabel idLabe;
    private javax.swing.JTextField idTextField;
    private javax.swing.JTable itemTable;
    private javax.swing.JPanel itemTablePanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel monthLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton newMonthButton;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JTextField priceTextField;
    private javax.swing.JLabel quantityLabel;
    private javax.swing.JTextField quantityTextField;
    private javax.swing.JButton updateQuantityButton;
    // End of variables declaration//GEN-END:variables

}
